//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;
using System.Collections;

/// <summary>
/// Allows dragging of the specified scroll view by mouse or touch.
/// </summary>

[ExecuteInEditMode]
[AddComponentMenu("NGUI/Interaction/Drag Scroll View")]
public class UIDragScrollView : MonoBehaviour
{
	/// <summary>
	/// Reference to the scroll view that will be dragged by the script.
	/// </summary>

	public UIScrollView scrollView;

	// Legacy functionality, kept for backwards compatibility. Use 'scrollView' instead.
	[HideInInspector][SerializeField] UIScrollView draggablePanel;

	/// <summary>
	/// Automatically find the scroll view if possible.
	/// </summary>

	void Start ()
	{
		// Auto-upgrade
		if (draggablePanel != null)
		{
			scrollView = draggablePanel;
			draggablePanel = null;
#if UNITY_EDITOR
			UnityEditor.EditorUtility.SetDirty(this);
#endif
		}

		if (scrollView == null)
			scrollView = NGUITools.FindInParents<UIScrollView>(gameObject);
	}

	/// <summary>
	/// Create a plane on which we will be performing the dragging.
	/// </summary>

	void OnPress (bool pressed)
	{
		if (enabled && NGUITools.GetActive(gameObject) && scrollView != null)
			scrollView.Press(pressed);
	}

	/// <summary>
	/// Drag the object along the plane.
	/// </summary>

	void OnDrag (Vector2 delta)
	{
		if (enabled && NGUITools.GetActive(gameObject) && scrollView != null)
			scrollView.Drag();
	}

	/// <summary>
	/// If the object should support the scroll wheel, do it.
	/// </summary>

	void OnScroll (float delta)
	{
		if (enabled && NGUITools.GetActive(gameObject) && scrollView != null)
			scrollView.Scroll(delta);
	}
}
